//
//  LOLfuwenGCollectionViewCell.m
//  LOLXXbox
//
//  Created by mac on 16/2/29.
//  Copyright © 2016年 Grasstrio. All rights reserved.
//

#import "LOLfuwenGCollectionViewCell.h"
#define WIDTH [[UIScreen mainScreen]bounds].size.width
#define HEIGHT [[UIScreen mainScreen]bounds].size.height
@implementation LOLfuwenGCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        [self createView];
    }
    return self;
}

-(void)createView
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.scrollView.delegate =self;
    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    self.scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT);
    [self.contentView addSubview:self.scrollView];

    
    
    [self.scrollView setMinimumZoomScale:1.0];
    [self.scrollView setMaximumZoomScale:3.0];
    
    
    
    
    self.skinImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/8,WIDTH/8)];
    self.skinImageView.center =self.scrollView.center;
    self.skinImageView.userInteractionEnabled = YES;
    [self.scrollView addSubview:self.skinImageView];

    
    UITapGestureRecognizer *signleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(signleTapAction:)];
    signleTap.numberOfTapsRequired = 1;
    
    [self.scrollView addGestureRecognizer:signleTap];

    
    
    
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapAction:)];
    doubleTap.numberOfTapsRequired = 2;
    
    [self.skinImageView addGestureRecognizer:doubleTap];

    
    
    
}





-(void)signleTapAction:(UITapGestureRecognizer *)tap
{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hiddenView" object:nil];
    
}

-(void)doubleTapAction:(UITapGestureRecognizer *)tap
{
    
    
    if (self.isBigSize&&self.isImageAppear) {
        
        float newScale = [self.scrollView zoomScale] / 3.0;
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[tap locationInView:tap.view]];
        [self.scrollView zoomToRect:zoomRect animated:YES];
        
        
        
    }
    else if(!self.isBigSize && self.isImageAppear)
    {
        
        float newScale = [self.scrollView zoomScale] * 3.0;
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[tap locationInView:tap.view]];
        [self.scrollView zoomToRect:zoomRect animated:YES];
        
        
        
    }
    self.isBigSize = !self.isBigSize;
    
    
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.skinImageView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    CGFloat xcenter = scrollView.center.x , ycenter = scrollView.center.y;
    //目前contentsize的width是否大于原scrollview的contentsize，如果大于，设置imageview中心x点为contentsize的一半，以固定imageview在该contentsize中心。如果不大于说明图像的宽还没有超出屏幕范围，可继续让中心x点为屏幕中点，此种情况确保图像在屏幕中心。
    xcenter = scrollView.contentSize.width > scrollView.frame.size.width ?
    scrollView.contentSize.width/2 : xcenter;
    //同上，此处修改y值
    ycenter = scrollView.contentSize.height > scrollView.frame.size.height ?
    scrollView.contentSize.height/2 : ycenter;
    [_skinImageView setCenter:CGPointMake(xcenter, ycenter)];
}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
    
    CGRect zoomRect;
    
    zoomRect.size.height = [self.scrollView frame].size.height / scale;
    zoomRect.size.width  = [self.scrollView frame].size.width  / scale;
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);
    
    return zoomRect;
}



@end
